package com.tronsis.library.widget;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.tronsis.library.R;


public class LoadingProgressDialog extends ProgressDialog {
    TextView mMessageTv;

    public LoadingProgressDialog(Context context) {
        this(context, R.style.CustomProgressDialog);
    }

    public LoadingProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init(getContext());
    }

    private void init(Context context) {
        //设置不可取消，点击其他区域不能取消，实际中可以抽出去封装供外包设置
        setCancelable(true);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.dialog_progress_loading);

        mMessageTv = this.findViewById(R.id.tv_load_message);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
    }

    @Override
    public void show() {
        super.show();
    }

    public void setMessage(String str) {
        mMessageTv.setText(str);
    }

    public void show(String message) {
        if (!this.isShowing()) {
            // 弹出对话框
            this.show();

            if (TextUtils.isEmpty(message)) {
                mMessageTv.setVisibility(View.GONE);
            } else {
                mMessageTv.setVisibility(View.VISIBLE);
                mMessageTv.setText(message);
            }

        }
    }
}